package com.syy.web.controller.system;
import com.syy.common.annotation.Mobile;
import com.syy.common.constant.Constants;
import com.syy.common.core.domain.AjaxResult;
import com.syy.common.core.domain.entity.SysMenu;
import com.syy.common.core.domain.entity.SysUser;
import com.syy.common.core.domain.model.LoginBody;
import com.syy.common.core.domain.model.LoginUser;
import com.syy.common.utils.SecurityUtils;
import com.syy.framework.pojo.CommonResult;
import com.syy.framework.web.service.SysLoginService;
import com.syy.framework.web.service.SysPermissionService;
import com.syy.framework.web.service.TokenService;
import com.syy.system.domain.vo.AuthSmsSendReqVO;
import com.syy.system.service.ISysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

import static com.syy.framework.pojo.CommonResult.success;

/**
 * 登录验证
 *
 * @author 地理信息中心
 */
@RestController
@Validated
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody){
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        //角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        System.out.println("roles = " + roles);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        System.out.println("permissions = " + permissions);
        if (!loginUser.getPermissions().equals(permissions))
        {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }


    // 阿里云发送验证码
    @PostMapping("/send-sms-code")
    @PermitAll
    @Operation(summary = "发送手机验证码")
    public CommonResult<Boolean> sendLoginSmsCode(@RequestBody @Valid AuthSmsSendReqVO reqVO) {
        loginService.sendSmsCode(reqVO);
        return success(true);
    }
    // 阿里云发送验证码
    @PostMapping("/send-aliyun-code")
    @PermitAll
    @Operation(summary = "阿里云发送手机验证码")
    public CommonResult<Boolean> sendLoginAliyunCode(@RequestParam @Mobile String phonenumber) {
        Boolean flag = loginService.sendLoginAliyunCode(phonenumber);
        return success(flag);
    }


}
